<template>
<el-container>
  <el-header style="height: auto;">
    <el-row class="back">
      <el-col>
        <el-button @click="toBack" size="small" type="primary">返回</el-button>
      </el-col>
    </el-row>
    <el-form :model="form" ref="form" :rules="formRules">
      <el-row>
        <el-col :xl="{ span: 4 }" :lg="{ span: 4 }">
          <el-form-item label="流量池名称" label-width="90px" prop="flowPoolName">
            <el-input size="small" style="width: 100%;" v-model="form.flowPoolName"></el-input>
          </el-form-item>
        </el-col>
        <el-col :xl="{ span: 4 }" :lg="{ span: 4 }">
          <el-form-item label="运营商" label-width="90px" prop="dictOperator">
            <el-select size="small" placeholder="请选择" style="width: 95%;" v-model="form.dictOperator">
              <el-option v-for="dictOperator in form.dictOperatorSelect" :key="dictOperator.id" :label="dictOperator.name" :value="dictOperator.id"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :xl="{ span: 4 }" :lg="{ span: 4 }">
          <el-form-item label-width="50px">
            <el-button size="small" type="primary" @click="currentPage = 0 ; initData()">查询</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </el-header>
  <el-main style="height: auto;">
    <el-form>
      <el-row>
        <el-col :xl="{ span: 4 }">
          <el-form-item>
            <el-button size="small" type="primary" @click="showTipPanel">批量设置权限数</el-button>
          </el-form-item>
        </el-col>
      </el-row>
      <el-table :header-cell-style="{ 'background':$store.state.app.tableBgColor,'color':'#606266' }" :data="tableData" tooltip-effect="dark" style="width: 100%" @selection-change="handleSelectionChange" v-loading.fullscreen="tableLoading" element-loading-text="拼命加载中" element-loading-spinner="el-icon-loading" element-loading-background="rgba(0, 0, 0, 0.2)" :row-key="getRowKeys">
        <el-table-column type="selection" align="center" :reserve-selection="true"></el-table-column>
        <el-table-column prop="flowPoolName" label="流量池名称" align="center">
          <template slot-scope="scope">
            <el-button type="text" @click="toUsage(scope.row)">{{ scope.row.flowPoolName }}</el-button>
          </template>
        </el-table-column>
        <el-table-column prop="firstpoolName" label="所属一级流量池" align="center"></el-table-column>
        <el-table-column prop="dictOperator" label="所属运营商" align="center"></el-table-column><!--el-table-column(-->
<!--prop="agentName"-->
<!--label="所属人"-->
<!--align="center")-->
        <el-table-column prop="theoryFlow" label="理论流量" align="center"></el-table-column>
        <el-table-column prop="containsFlow" label="实际流量" align="center"></el-table-column>
        <el-table-column prop="usedFlow" label="实际已使用" align="center"></el-table-column><!--el-table-column(-->
<!--prop="agentName"-->
<!--label="虚数已使用"-->
<!--align="center")-->
        <el-table-column prop="remainingFlow" label="剩余实际流量" align="center"></el-table-column>
        <el-table-column prop="cardNumber" label="挂卡数量" align="center"></el-table-column>
        <el-table-column prop="activationCardNumber" label="已开卡数" align="center">
          <template slot-scope="scope">
            <el-button type="text" @click="toCardList(scope.row,'ACTIVATE')">{{ scope.row.activationCardNumber }}</el-button>
          </template>
        </el-table-column><!--el-table-column(-->
<!--prop="agentName"-->
<!--label="平均单卡月用量"-->
<!--align="center")-->
        <el-table-column prop="unopenedCardNumber" label="未开卡数" align="center">
          <template slot-scope="scope">
            <el-button type="text" @click="toCardList(scope.row,'DEACTIVATE')">{{ scope.row.unopenedCardNumber }}</el-button>
          </template>
        </el-table-column><!--el-table-column(-->
<!--prop="agentName"-->
<!--label="单卡开卡金额"-->
<!--align="center")-->
<!--el-table-column(-->
<!--prop="agentName"-->
<!--label="总金额"-->
<!--align="center")-->
<!--el-table-column(-->
<!--prop="agentName"-->
<!--label="增加时间"-->
<!--align="center")-->
<!--el-table-column(-->
<!--prop="agentName"-->
<!--label="修改时间"-->
<!--align="center")-->
        <el-table-column prop="cao" label="操作" align="center">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="toUsage(scope.row)">查看用量统计</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="block"><span class="demonstration"></span>
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page.sync="currentPage" :page-size="10" layout="prev, pager, next, jumper" :page-count="maxPage" :hide-on-single-page="value" background></el-pagination>
      </div>
    </el-form>
  </el-main>
  <el-dialog class="delete" top="33vh" :visible.sync="isSettingImaginaryNumber" :append-to-body="true" width="30%">
    <el-form :model="a" :rules="b" ref="form1">
      <el-form-item label="设置虚数" label-width="80px" prop="cardFlowImaginary">
        <el-input size="small" v-model="a.cardFlowImaginary" v-has="'setImaginary'"></el-input>
      </el-form-item>
    </el-form><span class="dialog-footer" slot="footer">
      <el-button @click="isSettingImaginaryNumber = false" size="small">取消</el-button>
      <el-button type="primary" size="small" @click="showSecondaryPromptPanel('form1')">确定</el-button></span>
  </el-dialog>
  <el-dialog class="delete" top="33vh" :visible.sync="isCovers" :append-to-body="true" width="30%"><span>&nbsp;&nbsp;&nbsp;&nbsp;业务号码为 :</span><span style="display: flex; justify-content: center; color: red;">{{ this.cardNumber }}</span><span style="display: flex; justify-content: center;">&nbsp;&nbsp;的卡已经设置过虚数，是否重新设置虚数？</span><span class="dialog-footer" slot="footer">
      <el-button @click="submitSkipBatchSetImaginaryNumberHandle" size="small">跳过覆盖</el-button>
      <el-button type="primary" size="small" @click="submitForceBatchSetImaginaryNumberHandle">强制覆盖</el-button></span></el-dialog>
</el-container>
</template>

<script>
import { secondaryFlowPoolList, batchSettingImaginaryNumber, checkBatchSettingImaginaryNumber } from '@/api/getData'

export default {
  name: 'Next-Level',
  data() {
    const validateFlowPoolName = (rule, value, callback) => {
      if (value.length > 50) {
        callback(new Error('名称不能超过50位'))
      } else {
        callback()
      }
    }
    return {
      isSettingImaginaryNumber: false,
      isCovers: false,
      maxPage: 1,
      tableData: [],
      pageSize: 10,
      currentPage: 1,
      ascs: [],
      descs: [],
      value: false,
      tableLoading: false,
      form: {
        flowPoolName: '',
        dictOperator: '',
        dictOperatorSelect: []
      },
      a: {cardFlowImaginary:''},
      flowPoolName: '',
      dictOperator: '',
      theoryFlow: '',
      containsFlow: '',
      usedFlow: '',
      remainingFlow: '',
      cardNumber: '',
      activationCardNumber: '',
      unopenedCardNumber: '',
      firstPoolId: '',
      multipleSelection: [],
      id: '',
      isCover: '',
      cardFlowImaginary: '',
      cardList: [],
      b: {
        cardFlowImaginary: [
          { required: true, message: '请输入虚数', trigger: 'blur' },
          { pattern: /^(([1-9][0-9]*)|(([0]\.\d{0,1}|[1-9][0-9]*\.\d{0,1})))$/, message: '请输入数字,精确到小数点后一位', trigger: 'blur' }
        ]
      },
      formRules: {
        flowPoolName: [{ validator: validateFlowPoolName }]
      }
    }
  },
  created() {
    this.firstPoolId = this.$route.params.id
    this.initData()
  },
  methods: {
    getRowKeys(row) {
      return row.id
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    handleSizeChange(val) {
      //console.log(`每页 ${val} 条`)
      this.pageSize = val
    },
    handleCurrentChange(val) {
      //console.log(`当前页: ${val}`)
      this.currentPage = val
      this.initData()
    },
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done()
        })
        .catch(_ => {})
    },
    async initData() {
      const returnData = await secondaryFlowPoolList({
        firstPoolId: this.firstPoolId,
        page: {
          current: this.currentPage + '',
          size: this.pageSize + '',
          ascs: this.ascs,
          descs: this.descs
        }
      },
      {
        obj: this,
        key: 'tableLoading'
      })
      this.tableData = []
      if (returnData._result) {
        this.maxPage = Math.ceil(returnData.data.total / this.pageSize) // 分页的最大页数
        // 数据回显
        returnData.data.records.map(item => {
          this.form.dictOperatorSelect.push({ id: item.id, name: item.dictOperator })
          const obj = {}
          obj.id = item.id
          obj.flowPoolName = item.flowPoolName // 流量池名称
          obj.firstpoolName = item.firstpoolName // 流量池名称
          obj.dictOperator = item.dictOperatorName // 所属运营商
          obj.theoryFlow = item.theoryFlow // 理论流量
          obj.containsFlow = item.containsFlow // 实际流量
          obj.usedFlow = item.usedFlow // 实际已使用
          obj.remainingFlow = item.remainingFlow // 剩余实际流量
          obj.cardNumber = item.cardNumber // 挂卡数量
          obj.activationCardNumber = item.activationCardNumber // 已开卡数
          obj.unopenedCardNumber = item.unopenedCardNumber // 未开卡数
          obj.cardFlowImaginary = item.cardFlowImaginary // 设置过虚数的卡
          this.tableData.push(obj)
        })
        this.tableLoading = false
      }
    },
    // 显示提示面板
    showTipPanel() {
      if (this.multipleSelection.length !== 0) {
        this.isSettingImaginaryNumber = true
      } else {
        this.$message({ message: '请选择要设置虚数的卡组', type: 'error' })
      }
    },
    // 校验
    showSecondaryPromptPanel(form1) {
      this.$refs[form1].validate(async valid => {
        if (valid) {
          const returnData = await checkBatchSettingImaginaryNumber({
            ids: this.multipleSelection.map(item => item.id).join(','),
            cardFlowImaginary: this.a.cardFlowImaginary
          })
          if (returnData._result) {
            if (returnData.data.length !== 0) {
              this.isCovers = true
              this.cardNumber = returnData.data.map(item => item.cardNumber).join(',')
            } else {
              this.submitSkipBatchSetImaginaryNumberHandle()
            }
          }
        }
      })
    },
    // 跳过批量设置虚数
    async submitSkipBatchSetImaginaryNumberHandle() {

      const returnData = await batchSettingImaginaryNumber({
        ids: this.multipleSelection.map(item => item.id).join(','),
        cardFlowImaginary: this.a.cardFlowImaginary,
        isCover:'NOCOVER'
      })
      if (returnData._result) {
        this.isCovers = false
        this.isSettingImaginaryNumber = false
        this.$message({ message: '批量设置虚数成功', type: 'success' })
      }
    },
    // 强制覆盖
    async submitForceBatchSetImaginaryNumberHandle() {
      const returnData = await batchSettingImaginaryNumber({
        ids: this.multipleSelection.map(item => item.id).join(','),
        cardFlowImaginary: this.a.cardFlowImaginary,
        isCover:'COVER'
      })
      if (returnData._result) {
        this.isCovers = false
        this.isSettingImaginaryNumber = false
        this.$message({ message: '批量设置虚数成功', type: 'success' })
      }
    },
    toBack() {
      this.$router.back(-1)
    },
    toUsage() {
      this.$router.push({ path: '/package/usage' })
    },
    toCardList(secondPool,activeStatus) {
      let id = secondPool.id
      let active = activeStatus
      this.$router.push({ path: '/package/next-card-list/'+id+'/'+active})
    }
  }
}
</script>

<style scoped lang="scss">
.el-container {
  .el-header {
    margin-top: 5vh;
    .back {
      margin-bottom: 3vh;
    }
  }
  .el-main {
    .block {
      float: right;
    }
  }
}
</style>
